//
//  USLocalNotificationView.m
//  usale0001
//
//  Created by mini on 16/9/5.
//  Copyright © 2016年 YU. All rights reserved.
//

#import "USLocalNotificationView.h"

@interface USLocalNotificationView()

@property (nonatomic, copy) NSString *body;
@property (nonatomic, assign) BOOL isDismiss;
@end

@implementation USLocalNotificationView

- (instancetype)init
{
    if (self = [super initWithFrame:CGRectMake(0, -84, kUSScreenWidth, 64)]) {
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithBody:(NSString *)body
{
    if (self = [self init]) {
        self.body = body;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    //self.backgroundColor = [UIColor whiteColor];
    //添加毛玻璃
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0 , self.frame.size.width, self.frame.size.height)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [self addSubview:toolbar];
    
    UIImageView *imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"usale_icon"];
    imageView.layer.cornerRadius = 3;
    imageView.layer.masksToBounds = YES;
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(8);
        make.top.equalTo(self).offset(24);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:13];
    label.text = self.body;
    label.numberOfLines = 2;
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(5);
        make.centerY.equalTo(imageView);
        make.right.equalTo(self).offset(-8);
    }];
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.layer.cornerRadius = 2;
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(4);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-4);
    }];
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    __weak typeof(self) weakSelf = self;
   [UIView animateWithDuration:0.4 animations:^{
       CGRect frame = weakSelf.frame;
       frame.origin.y = 0;
       weakSelf.frame = frame;
   }completion:^(BOOL finished) {
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           if (!_isDismiss) {
               [weakSelf dismiss];
           }
        });
   }];
}

- (void)dismiss
{
     __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.4 animations:^{
        CGRect frame = weakSelf.frame;
        frame.origin.y = - frame.size.height;
        weakSelf.frame = frame;
    }completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.isDismiss = YES;
    [self dismiss];
}

@end
